package com.gui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JComponent;
import com.math.Point2D;
/**
* @author Dror
*
* email: gumjum.o.o@gmail.com
*
*/
public class Joystick extends JComponent implements MouseMotionListener,MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
int r=100;
Point2D j;
int jr;
Point2D c;
int cr;
Point2D p;
Point2D lp;
int [][] sx;
int [][] sy;
public Joystick(int a) {
super();
this.addMouseListener(this);
this.addMouseMotionListener(this);
r = a;
build();
}
public Joystick() {
this(50);
}
public void build(){
j = new Point2D();
c = new Point2D();
p = new Point2D();
lp = new Point2D();
j.x=r;
j.y=r;
c.x=r;
c.y=r;
jr=r/4+r/10;
cr = r/4;
sx = new int [2][4];
sy = new int [2][4];
}
public void normalize(){
this.setPreferredSize(new Dimension(2*r,2*r));
// this.setSize(w, h);
}
public void rebuild(){
sx[0][0] = c.x + 5;
sx[0][1] = c.x - 5;
sx[0][2] = j.x - 5;
sx[0][3] = j.x + 5;
sy[0][0] = c.y + 5;
sy[0][1] = c.y - 5;
sy[0][2] = j.y - 5;
sy[0][3] = j.y + 5;
sx[1][0] = c.x + 5;
sx[1][1] = c.x - 5;
sx[1][2] = j.x - 5;
sx[1][3] = j.x + 5;
sy[1][0] = c.y - 5;
sy[1][1] = c.y + 5;
sy[1][2] = j.y + 5;
sy[1][3] = j.y - 5;
}
public void paintComponent(Graphics g){
rebuild();
g.setColor(Color.gray);
g.fillArc(c.x-r, c.y-r, 2*r, 2*r, 0, 360);
g.setColor(Color.black);
g.fillArc(c.x-cr, c.y-cr, 2*cr, 2*cr, 0, 360);
g.setColor(Color.lightGray);
g.fillPolygon(sx[0], sy[0], 4);
g.fillPolygon(sx[1], sy[1], 4);
Color c = Color.blue;
int rr = c.getRed()+10,gg = c.getGreen()+10,bb = c.getBlue()+10;
for(int i = 0;i<jr-3;i++){
rr += +5;
gg += +5;
bb += +5;
if(rr < 0)
rr = 0;
if(gg < 0)
gg = 0;
if(bb < 0)
bb = 0;
if(rr > 255)
rr = 255;
if(gg > 255)
gg = 255;
if(bb > 255)
bb = 255;
g.setColor(new Color(rr,gg,bb));
g.fillArc(j.x-jr+i, j.y-jr+i, 2*(jr-i), 2*(jr-i), 0, 360);
}
}
@Override
public void mouseDragged(MouseEvent e) {
lp.set(p);
p.x = -c.x + e.getX();
p.y = -c.y + e.getY();
double sq = p.length();
int maxr = r-jr;
if(sq<maxr){
j.x = p.x + c.x;
j.y = p.y + c.y;
}else{
p.x = (int)(p.x/sq * maxr);
p.y = (int)(p.y/sq * maxr);
j.x = p.x + c.x;
j.y = p.y + c.y;
}
// this.dispatchEvent(new ActionEvent(this,ActionEvent.ACTION_PERFORMED, p.x+","+p.y));
// this.dispatchEventImpl(new Event(this,ActionEvent.ACTION_PERFORMED, px+","+py));
this.firePropertyChange("joystick", lp, p);
}
@Override
public void mouseMoved(MouseEvent arg0) {
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
j.x = c.x;
j.y = c.y;
p.set(0,0);
this.firePropertyChange("joystick", lp, p);
}
}